<?php

class LogTransactionPeer extends BaseLogTransactionPeer
{
  static function getReceivables($customer_iden){
  
    $c = new Criteria();
    $c->add(VReceivablesPeer::IDEN1,$customer_iden);
    $c->add(VReceivablesPeer::DEBIT,'debit-credit > 0',Criteria::CUSTOM);
    return VReceivablesPeer::doSelect($c);
  }
  
  static function getInvoiceBalance($customer_iden,$invoice_iden){
      $connection = Propel::getConnection();
      $query = 
        'SELECT sum(debit-credit) AS total '.
        'FROM log_transaction '.
        'join document on document.document_iden = log_transaction.document_iden and document.void=\'N\' '.
        'WHERE iden2 =\''.$invoice_iden.'\' '.
        'and type2=\'INVOICE\' '.
        'and iden1=\''.$customer_iden.'\' ';
        'and type1=\'CLIENT\' '.
        'and transaction_type=\'INVOICE\' ';
      $statement = $connection->prepare($query);
      $statement->execute();
      $logTransactionTotal = $statement->fetchAll(PDO::FETCH_ASSOC);
      return $logTransactionTotal[0]['total'];
  }
  
}
